/**
 * <p>文件名:		CommonLogFactory.java</p>
 * <p>版权:		CopyrightTag</p>
 * <p>公司:		千方集团CTFO</p>
 * @author		周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 */

package com.caits.lbs.framework.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.caits.lbs.framework.services.jmx.ILogSetable;
import com.caits.lbs.framework.services.jmx.JMXManager;

/**
 * <p>
 * CommonLogFactory
 * </p>
 * <p>
 * 用途：通用日志工厂类
 * 用来产生Slf4j的日志接口类，异常数据日志，可以动态设置日志级别
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.1 2012-6-13
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2012-6-13 上午9:41:29</td>
 *          </tr>
 *          <tr>
 *          <td>0.0.1</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2012-6-13 上午9:41:29</td>
 *          <td>0.0.2</td>
 *          <td>修改类</td>
 *          <td>xxx</td>
 *          <td>x年x月x日</td>
 *          </tr>
 *          </table>
 */
public class CommonSlf4jLogFactory  implements ILogSetable {
	/** 普通日志记录器 */
	private static final Logger log = LoggerFactory.getLogger(CommonSlf4jLogFactory.class);
	/** 中间数据记录器 */
	private static final Logger errDataLog;
	
	/** 程序异常消息日志记录器 */
	private static final Logger exceptionLog;
	
	/** 自身单一实例 */
	private static CommonSlf4jLogFactory instance=null;
	static{
		errDataLog = LoggerFactory.getLogger("errordatafile");
		exceptionLog = LoggerFactory.getLogger("exceptionLog");
	}
	/**
	 * 构造器 
	 */
	protected CommonSlf4jLogFactory() {
		Assert.isNull(instance, "CommonLogFactory实例已经创建.");
		instance = this;
		JMXManager jmx = JMXManager.getInstance();
		if(jmx!=null)jmx.addObject(this);
	}
	@Override
	public String setLevel(int level) {
		log.warn(String.format("%s实例设置普通日志级别为%d",toString(),level));
		return log.getName();
	}
	
	public String getLevel() {
		return log.getName();
	}
	/**
	 * 设置错误数据日志级别 
	 * @param level
	 * @return
	 */
	public String setErrorLogLevel(int level) {
		log.warn(String.format("%s实例设置errDataLog日志级别为%d",toString(),level));
		return errDataLog.getName();
	}
	/**
	 * 获取错误数据日志级别 
	 * @return
	 */
	public String getErrorLogLevel() {
		return errDataLog.getName();
	}
	/**
	 * 设置任意日志级别 
	 * @param name
	 * @param level
	 * @return
	 */
	public String setAnyLogLevel(String name,int level) {
		log.warn(String.format("%s实例设置%s日志级别为%d",toString(),name,level));
		Logger lg = LoggerFactory.getLogger(name);
		return lg.getName();
	}
	/**
	 * 获取任意日志级别 
	 * @param name
	 * @return
	 */
	public String getAnyLogLevel(String name) {
		Logger lg = LoggerFactory.getLogger(name);
		return lg.getName();
	}
	/**
	 * 获取指定日志名称的对象 
	 * @param name
	 * @return
	 */
	public Logger getAnyLog(String name) {
		return LoggerFactory.getLogger(name);
	}
	/**
	 * 获取变量<code>errdatalog</code>的值
	 * @return 返回的数据类型是<code>Logger</code>
	 */
	public static Logger getErrdatalog() {
		return errDataLog;
	}
	/**
	 * 获取变量<code>instance</code>的值
	 * @return 返回的数据类型是<code>CommonLogFactory</code>
	 */
	public static synchronized CommonSlf4jLogFactory getInstance() {
		if(instance==null){
			instance = new CommonSlf4jLogFactory();
		}
		return instance;
	}
	public static Logger getLog() {
		return log;
	}
	/**
	 * 获取变量<code>exceptionlog</code>的值
	 * @return 返回的数据类型是<code>Logger</code>
	 */
	public static Logger getExceptionlog() {
		return exceptionLog;
	}
	
}
